# coding=utf-8

import unittest
from lib import TestReport, sendmail, config_reader
import time
import os
from openmind_hub import set_platform

def run(pattern,Subject):
    test_dir = r'/openmind-hub/auto_test/autotest/test_openmind_hub'
    suite = unittest.defaultTestLoader.discover(start_dir=test_dir, pattern=pattern)
    now = time.strftime('%Y-%m-%d_%H_%M_%S')
    reportname = r'/openmind-hub/auto_test/autotest/report/testreport/' + 'TestResult' + now + '.html'
    with open(reportname, 'wb') as f:
        runner = TestReport.HTMLTestReport(
            stream=f,
            title='测试报告',
            description='Test the import testcase',
            images=True
        )
        print("用例开始运行")
        runner.run(suite)
        print("用例运行全部结束")
    time.sleep(3)
    # 发送邮件
    mail = sendmail.SendMail()
    mail.send(config,Subject)


if __name__ == '__main__':
    print("读取config文件内容")
    global config
    config = config_reader.read_config()
    # token_modelers = config.get("'DEFAULT'", "'token_modelers'")
    # token_giteeAI = config.get("'DEFAULT'", "'token_giteeAI'")
    # token_huggingface = config.get("'DEFAULT'", "'token_huggingface'")
    # token_gitcode = config.get("'DEFAULT'", "'token_gitcode'")
    # os.remove(r"/vault/secrets/hub.ini")
    # print("读取config文件内容完成")

    # # "替换gitee AI社区用例的token值"
    # giteeAIcase_list = ["/openmind-hub/auto_test/autotest/test_openmind_hub/test_thirdparty_giteeAI.py"]
    # for i in giteeAIcase_list:
    #     with open(i, 'r', encoding='utf-8') as f:
    #         f_content = f.read()
    #         f_content = f_content.replace('123456789()', token_giteeAI)
    #     with open(i, 'w', encoding='utf-8') as f:
    #         f.write(f_content)

    os.environ["HUB_WHITE_LIST_PATHS"] = "/openmind-hub"
    os.environ["HF_ENDPOINT"] = "https://hf-api.gitee.com"
    set_platform("gitee")
    run("test_thirdparty_giteeAI.py", "giteeAI社区工具链测试报告")



